package cn.com.cifi.mars.web.action.sys;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONArray;

import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.bean.vo.sys.req.EnableSysPermissionReqParam;
import cn.com.cifi.mars.bean.vo.sys.req.SaveSysPermissionReqParam;
import cn.com.cifi.mars.bean.vo.sys.req.UpdateSysPermissionReqParam;
import cn.com.cifi.mars.bean.vo.sys.rsp.UserFunctionalPermissionVo;
import cn.com.cifi.mars.common.base.BaseAction;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.constant.LogEventEnum;
import cn.com.cifi.mars.constant.LogSrcEnum;
import cn.com.cifi.mars.entity.common.IdmUser;
import cn.com.cifi.mars.service.IdmUserService;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.service.SysPermissionService;
import cn.com.cifi.mars.util.WebUtils;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author yyd
 * @since 2019-09-18
 */
@RestController
public class SysPermissionController extends BaseAction implements ISysPermissionController{

	@Autowired
	private SysPermissionService sysPermissionService;	
	@Autowired
	private SysLogService sysLogService;
	@Autowired
	private IdmUserService idmUserService;
	
	
	/**
	 * 添加菜单(权限)
	 */
	@Override
	public R<String> createSysPermission(String apiVersion, SaveSysPermissionReqParam param) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();

		IdmUser user = getOperator();
		// 新增日志
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.ADD);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("菜单管理");
		bo.setTitle("新建菜单");
		bo.setContent("新建菜单");
		boolean msg = sysLogService.add(bo, user.getAlias());
		return ok(sysPermissionService.add(param, user.getAlias()));
	}
	
	/**
	 * 编辑菜单(权限)
	 */
	@Override
	public R<String> updateSysPermission(String apiVersion, UpdateSysPermissionReqParam param) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();

		IdmUser user = getOperator();
		// 新增日志
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.EDIT);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("菜单管理");
		bo.setTitle("编辑菜单");
		bo.setContent("编辑菜单");
		boolean msg = sysLogService.add(bo, user.getAlias());
		return ok(sysPermissionService.update(param, user.getAlias()));
	}

	/**
	 * 禁用启用菜单(权限)
	 */
	@Override
	public R<String> updateEnableSysPermission(String apiVersion, EnableSysPermissionReqParam param) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();

		IdmUser user = getOperator();
		// 新增日志
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.EDIT);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("菜单管理");
		bo.setTitle("禁用启用菜单(权限)");
		bo.setContent("禁用启用菜单(权限)");
		boolean msg = sysLogService.add(bo, user.getAlias());
		return ok(sysPermissionService.enable(param));
	}

	/**
	 * 查询菜单权限树
	 */
	@Override
	public R<JSONArray> getSysPermissionTree(String apiVersion) {
		return ok(sysPermissionService.querySysPermissionTree("",""));
	}
	
	/**
	 * 当前用户的菜单权限
	 */
	@Override
	public R<UserFunctionalPermissionVo> getCurrentUserPermissions(String apiVersion) {
		IdmUser user = getOperator();
		return ok(idmUserService.queryUserFunctionalPermissions(user.getUsername()));
	}

}
